package dao;

import pojo.Product;
import utils.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 郝雷蒙
 * @Description
 * @date 2020/11/26 9:09
 */
public class ProductDaoImpl implements ProductDao {

    @Override
    public List<Product> selectAllProducts(String order) {
        if (order.equals("a")){
            order = " order by sumprise asc";
        }else if(order.equals("b")){
            order = " order by sumprise desc";
        }else if(order.equals("c")){
            order = " order by p.price asc";
        }else {
            order = " order by p.price desc";
        }

        List<Product> products = new ArrayList<>();
        String sql = "select p.*,c.number,(p.price*c.number) as sumprise from product p inner join cart c on p.productId = c.productId "+order;
        System.out.println(sql);
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = jdbcUtil.executeQuery(sql);
        try {
            while (resultSet.next()) {
                Product product = new Product();
                product.setProductId(resultSet.getInt("productId"));
                product.setProductName(resultSet.getString("productName"));
                product.setPrice(resultSet.getDouble("price"));
                product.setNumber(resultSet.getInt("number"));
                product.setSumprice(resultSet.getDouble("sumprise"));

                products.add(product);
            }

            return products;
        } catch (SQLException e) {
            e.printStackTrace();

            return null;
        } finally {
            jdbcUtil.close();
        }
    }


    @Override
    public Product selectAllSumPrice() {
        String sql = "select SUM(p.price*c.number) as allSumPrice from product p inner join cart c on p.productId = c.productId  ";

        System.out.println(sql);
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = jdbcUtil.executeQuery(sql);
        System.out.println(resultSet);
        try {
            if (resultSet.next()){
                Product product =new Product();
                product.setAllSumPrice(resultSet.getDouble("allSumPrice"));
                return product;
            }else{
                return null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }finally {
            jdbcUtil.close();
        }
    }


    @Override
    public Integer deleteAllProducts(List<Integer> dIds) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "delete from product where productId in ";
        StringBuffer buffer = new StringBuffer();

        for (Integer dId : dIds) {
            if (buffer.length() > 0) {
                buffer.append(",");
            }

            buffer.append(dId);
        }

        sql += "(" + buffer.toString() + ")";

        System.out.println(sql);

        Integer status = jdbcUtil.executeUpdate(sql, false);

        jdbcUtil.close();

        return status;
    }

    @Override
    public Integer updateOneproduct(Product product) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "update cart set number=? where productId=? ";
        Integer status = jdbcUtil.executeUpdate(sql, false, product.getNumber(), product.getProductId());
        jdbcUtil.close();
        return status;
    }


    @Override
    public Product selectproductById(Integer did) {
        List<Product> products = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "select p.*,c.number,(p.price*c.number) as sumprise from product p inner join cart c on p.productId = c.productId where p.productId=? ";
        ResultSet resultSet = jdbcUtil.executeQuery(sql, did);
        try {
            while (resultSet.next()) {
                Product product = new Product();

                product.setProductId(resultSet.getInt("productId"));
                product.setProductName(resultSet.getString("productName"));
                product.setPrice(resultSet.getDouble("price"));
                product.setNumber(resultSet.getInt("number"));
                product.setSumprice(resultSet.getDouble("sumprise"));

                products.add(product);
            }

            return products.size() > 0 ? products.get(0) : null;
        } catch (SQLException e) {
            e.printStackTrace();

            return null;
        } finally {
            jdbcUtil.close();
        }
    }
}
